package com.highcom.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.highcom.entity.PageResult;
import com.highcom.entity.QueryPageBean;
import com.highcom.mapper.CheckItemDao;
import com.highcom.model.CheckItem;
import com.highcom.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckItemServiceImpl  implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> pages = checkItemDao.selectByItem(queryString);
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        //判断当前检查项是否已经关联检查组
        long countByCheckItemId = checkItemDao.findCountByCheckItemId(id);
        if (countByCheckItemId >0){
            new RuntimeException();
        }

        checkItemDao.deleteById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
